package org.bite.book_controller.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.bite.book_controller.constant.Constants;
import org.bite.book_controller.model.UserInfo;
import org.bite.book_controller.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/user")
@RestController
public class userController {
    @Autowired
    private  UserService userService;

    @RequestMapping("/login")
    public  String login(String userName, String passWord, HttpSession session) {
        log.info("用户登录 userName:{},password:{}",userName,passWord);
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(passWord)){
            return "用户名不能为空";
        }

        UserInfo userInfo = userService.queryUserByName(userName);
        if(userInfo == null || userInfo.getId() < 0){
            return "用户不存在";
        }
        if(!passWord.equals(userInfo.getPassword())){
            return "密码不正确";
        }
//        if(!"zhangsan".equals(userName) || !"123456".equals(passWord)){
//            return"账号密码错误";
//        }
        userInfo.setPassword("");
        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
        return "登录成功";
    }
}
